package com.wish233.tolerant.impl;

import com.wish233.model.rpc.RpcResponse;
import com.wish233.proxy.MockServiceProxy;
import com.wish233.tolerant.TolerantStrategy;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 降级到其他服务 - 容错策略
 *
 * @author WHH
 */
@Slf4j
public class FailBackTolerantStrategy implements TolerantStrategy {

    @Override
    public RpcResponse doTolerant(Map<String, Object> context, Exception e) {
        // 降级到MOCK
        Class<?> returnType = (Class<?>) context.get("returnType");
        Object obj = MockServiceProxy.fakeReturnObj(returnType);
        RpcResponse response = new RpcResponse();
        response.setData(obj);
        response.setDataType(returnType);
        return response;
    }
}
